<template>
  <div class='xtx-goods-page' v-if="obj">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/" >{{obj.categories[1].name}}</XtxBreadItem>
        <XtxBreadItem to="/" >{{obj.categories[0].name}}</XtxBreadItem>
        <XtxBreadItem to="/">{{obj.name}}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info">

         <div class="media">
          <GoodsImage :arr="obj.mainPictures" />
          <GoodsSales />
        </div>
        <div class="spec">
          <GoodsName :goods="obj"/>
          <GoodsSku :goods='obj' :sukid='obj.sukid' @change="getdata"  ></GoodsSku>
          <XtxNumbox v-model='num' Text=数量 :max='obj.inventory'></XtxNumbox>
          <!-- <XtxButton style="margin-top:20px">加入购物车</XtxButton> -->
          <XtxButton @click="addToCartBtn" type="primary" style="margin-top:20px">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品推荐 -->
      <GoodsRelevant />
      <!-- 商品详情 -->
      <div class="goods-footer">

        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsTabs></GoodsTabs>

          </div>
          <!-- 注意事项 -->
          <div class="goods-warn">
            <GoodsWarn></GoodsWarn>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="1" :id="obj.id"></GoodsHot>
          <GoodsHot :type='2' :id="obj.id"></GoodsHot>
        </div>
      </div>
    </div>
  </div>

   <div v-else class="loading" />
</template>

<script>
import Message from '@/components/library/Message'
import GoodsWarn from './components/goods-warn.vue'
import GoodsHot from './components/goods-hot.vue'
import GoodsTabs from './components/goods-tabs.vue'
import GoodsRelevant from './components/goods-relevant.vue'
import { useStore } from 'vuex'
import { goodsedit } from '@/api/goods'
import { watch, ref, provide } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
export default {
  name: 'XtxGoodsPage',
  components: { GoodsRelevant, GoodsImage, GoodsSales, GoodsName, GoodsSku, GoodsTabs, GoodsHot, GoodsWarn },
  setup () {
    const store = useStore()
    const currSkuId = ref('')
    const num = ref(0)
    const obj = ref(null)
    const route = useRoute()
    watch(() => route.params.id, async (news, old) => {
      obj.value = null
      const { result } = await goodsedit(news)
      obj.value = result
    }, { immediate: true })

    const getdata = (v) => {
      if (v.skuId) {
        console.log(obj.value.inventory)
        obj.value.attrsText = v.attrsText
        obj.value.oldPrice = v.oldPrice
        obj.value.price = v.price
        obj.value.inventory = v.inventory
        currSkuId.value = v.skuId
      } else {
        currSkuId.value = ''
      }
    }
    const addToCartBtn = () => {
      // 判断是否选择了完整规格
      if (!currSkuId.value) return Message('warn', '请选择完整规格')
      if (num.value > obj.value.inventory) return Message('warn', '库存不足了')
      // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
      // 编写mutaions添加购物车逻辑 - 修改state的函数
      // 编写actions进行添加操作 - 考虑到后期需要异步修改，所以再封装 actions
      // 在商品详情页实现添加逻辑
      // 约定本地存储的信息内容 - 字段名可参考购物车列表接口的字段名
      const cartItem = {
        id: obj.value.id,
        name: obj.value.name,
        picture: obj.value.mainPictures[0],
        price: obj.value.price,
        skuId: currSkuId.value,
        attrsText: obj.value.attrsText,
        nowPrice: obj.value.price,
        stock: obj.value.inventory || obj.value.stock, // 库存字段兼容性写法(库存在不同接口字段名有差别)
        count: num.value,
        selected: true,
        isCollect: false,
        isEffective: true,
        discount: 1
      }

      console.log(obj, cartItem)

      // 编写mutaions添加购物车逻辑 - 修改state的函数
      // 编写actions进行添加操作 - 考虑到后期需要异步修改，所以再封装 actions
      // 添加到 VueX 仓库中，VueX 数据做了本地存储同步，所以可以通过本地存储查看数据
      // store.commit('cart/insertCart', cartItem)
      store.dispatch('cart/insertCart', cartItem).then(res => {
        Message('success', res)
      })
      // 在商品详情页实现添加逻辑
      console.log('点击了加入购物车逻辑')
    }

    provide('goods', obj)
    return { obj, num, getdata, addToCartBtn }
  }
}
</script>

<style scoped lang='less'>
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;
  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }
  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}
.goods-footer {
  display: flex;
  margin-top: 20px;
  .goods-article {
    width: 940px;
    margin-right: 20px;
  }
  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}
.goods-tabs {
  min-height: 600px;
  background: #fff;
}
.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
.loading{
  height: 500px;
  background: white url(~@/assets/images/loading.gif) no-repeat center;
}
</style>
